package controller;

import java.util.Vector;

import view.MainWindow;
import model.Tower;

public class GameController implements Runnable{
	private MainWindow window;
	private MouseHandler mouse;
	private KeyboardHandler keyboard;
	private Vector<Tower> towerList;

	public GameController(){
		initModelComponents();
		initViewComponents();
		initControllerComponents();
	}

	private void initViewComponents(){
		this.window = new MainWindow(800,800,towerList);
		window.setVisible(true);
	}
	
	private void initControllerComponents(){
		this.mouse = new MouseHandler(towerList);
		this.keyboard = new KeyboardHandler();
		this.window.addMouseListener(this.mouse);
		this.window.addKeyListener(this.keyboard);
	}
	
	private void initModelComponents(){
		towerList = new Vector<Tower>();
		
	}
	
	public void run(){
        long beforeTime, timeDiff, sleep;
        beforeTime = System.currentTimeMillis();
		while(true){
            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = 5 - timeDiff;

            if (sleep < 0)
                sleep = 1;

            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                System.out.println("Interrupted: " + e.getMessage());
            }

            beforeTime = System.currentTimeMillis();
            window.step();	
		}
		
	}
}
